package com.zhangwsh.springboot.springbootdemo.controller;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

import static cn.hutool.core.date.DatePattern.NORM_DATETIME_MS_FORMAT;

/**
 * @Classname TestController
 * @Description TODO
 * @Date 2020/6/4 16:32
 * @Author zhangwsh
 */
@RestController
@RequestMapping("say")
@Slf4j
public class TestController {

    @RequestMapping("hello")
    public String getValue() {
        log.info("何小路,你最帅......");
        return "hello hexiaolu 10:13";
    }

    @RequestMapping("nice")
    public String hello(String name) {
        String time = DateUtil.format(new Date(), DatePattern.NORM_DATETIME_MS_FORMAT);
        String resp = time + " say good to " + name;
        log.info("receive:{},response:{}", name, resp);
        return resp;
    }

    /**
     * 因为是同一个网卡采用overlay的形式，所以可以采用服务名进行访问
     *
     * @return
     */
    @RequestMapping("hello2")
    public String getValue2() {
        log.info("h2 何小路,你最帅......");
        String result = HttpUtil.get("http://192.168.163.150:8081/say/hello");
        String result2 = HttpUtil.get("http://springboot2:8081/say/hello");
        return result + "" + result2;
    }

    @RequestMapping("hello3")
    public String getValue3() {
        log.info("请求3......");
        String a = "3333";
        synchronized (a) {
            log.info("进入3......");
            try {
                Thread.sleep(10000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return "hello3";
    }

    @RequestMapping("hello4")
    public String getValue4() {
        log.info("请求4......");
        String b = "3333";
        synchronized (b) {
            log.info("进入4......");
            try {
                Thread.sleep(10000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return "hello3";
    }
}
